const apis = require("../../apis/request")
const app = getApp();
const utils = require('../../utils')
const { ttpay, retry } = require('../../utils/pay')
let applyStatus = false
Page({
  data: {
    isFullScreen: false,
    countDownTime: '2099.12.15 13:14',
    expandStatus: false,
    imgList: [
      'https://img.wlcod.com/red-packet-mall/img/111 1@2x.webp',
      'https://img.wlcod.com/red-packet-mall/img/222 1@2x.webp',
      'https://img.wlcod.com/red-packet-mall/img/333 1@2x.webp',
    ],
    bannerList: [
      {
        id: 1,
        img: 'https://img.wlcod.com/red-packet-mall/img/banner1.webp'
      },
      {
        id: 2,
        img: 'https://img.wlcod.com/red-packet-mall/img/banner2.webp'
      }
    ],
    dataList: [],
    dataItem: {},
    isDisabled: true,
    swiperItem: 0
  },
  async onLoad(o) {
    const { data } = await apis.getRedEnvelopeCoversDetail(o.id)
    const displayTime = !(new Date(data.rushEndTime) - Date.now())
    data.carousel = data.carousel.map(item => {
      return {
        url: item,
        isVideo: item.endsWith('mp4')
      }
    })
    this.setData({
      dataItem: data,
      countDownTime: displayTime ? '0' : data.rushEndTime,
      isDisabled: displayTime,
      dataList: await this.getList()
    })
  },
  onShow() {
    this.refreshScreenRatio();
  },
  onResize(res) {
    this.setData({ isFullScreen: res.screenRatio === 1 })
  },
  onReady() {
    this.refreshScreenRatio();
  },
  refreshScreenRatio() {
    this.setData({ isFullScreen: app.utils.isFullScreen() })
  },
  expand() {
    this.setData({ expandStatus: !this.data.expandStatus })
  },
  bindswiperchange(e) {
    this.setData({ swiperItem: e.detail.current })
  },
  async pay() {
    if (applyStatus) return
    applyStatus = true
    tt.showToast({ title: '加载中', icon: 'none' });
    const r = await utils.getOpenIdOrCode()
    const params = {
      ...r,
      liveRoomId: app.globalData.query.live_room_id,
      orderableId: this.data.dataItem.id,
      orderableType: "red_envelope_covers",
      generalTrade: true
    }
    const { data: order } = await apis.getRedEnvelopeOrders(params)
    applyStatus = false

    console.log('本地orderid', order);
    const { status, orderId, isPay } = await ttpay(order.generalTradeRequestOrderOptions)
    if (!isPay) {
      console.log('用户主动取消支付')
      const r = await utils.getOpenIdOrCode()
      return apis.ordersCancel({
        id: order.id,
        ...r
      })
    }
    const res = await retry(async () => {
      const { data } = await apis.getRedEnvelopeOrdersQuery(order.id)
      if (data.status === 0) throw new Error('支付未完成')
      return data
    }, 10, 1000)
    tt.hideLoading();
    if (res.status === 1) {
      tt.showToast({
        title: '支付成功',
        icon: 'none'
      });
    } else {
      tt.showToast({
        title: '支付失败',
        icon: 'none'
      });
    }

  },
  async getList() {
    const { data } = await apis.getRedEnvelopeCovers({ hot: 1, pageSize: 100 })
    return data
  },
  onItemClick(e) {
    const id = e.currentTarget.dataset.item.id
    tt.redirectTo({ url: `/pages/goodsDetail/goodsDetail?id=${id}` });
  },
  timeOut() {
    this.setData({ isDisabled: true })
  },
})